package com.inossem.warehousestorage_inossem.activity.group_up_frame;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.zhy.android.percent.support.PercentLinearLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：wsy on 2018/1/24 10:30
 * <p>
 * 邮箱：445979770@qq.com
 */

public class GroupUpFrameOverviewActivity extends BaseActivity{

    private PercentLinearLayout documentOverview , groupMaterial , submit;
    private ImageView documentOverviewImage , groupMaterialImage , submitImage;
    private TextView documentOverviewText , groupMaterialText , submitText;
    private TextView title;
    private FragmentManager fragmentManager;
    private static final String STATE_FRAGMENT_SHOW = "stateFragmentShow";
    private Fragment currentFragment = new Fragment();
    private List<Fragment> fragments = new ArrayList<>();
    private int currentIndex = 0;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.activity_group_up_frame_overview);
        initView(GroupUpFrameOverviewActivity.this);
        initData(GroupUpFrameOverviewActivity.this , savedInstanceState);
    }

    private void initView(Activity activity) {
        documentOverview = (PercentLinearLayout) activity.findViewById(R.id.documentOverview);
        groupMaterial = (PercentLinearLayout) activity.findViewById(R.id.groupMaterial);
        submit = (PercentLinearLayout) activity.findViewById(R.id.submit);
        documentOverviewImage = (ImageView) activity.findViewById(R.id.documentOverviewImage);
        groupMaterialImage = (ImageView) activity.findViewById(R.id.groupMaterialImage);
        submitImage = (ImageView) activity.findViewById(R.id.submitImage);
        documentOverviewText = (TextView) activity.findViewById(R.id.documentOverviewText);
        groupMaterialText = (TextView) activity.findViewById(R.id.groupMaterialText);
        submitText = (TextView) activity.findViewById(R.id.submitText);
        title = (TextView) activity.findViewById(R.id.headerTitle);
    }
    private void initData(Activity activity , Bundle savedInstanceState) {
        title.setText(R.string.activity_group_up_frame_overview_document_overview);
        ActivityUtils.initBackAndFunction(activity, R.string.activity_in_storage_receive_list_title_short, new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        fragmentManager = getFragmentManager();
        if (savedInstanceState != null) { // “内存重启”时调用

            //获取“内存重启”时保存的索引下标
            currentIndex = savedInstanceState.getInt(STATE_FRAGMENT_SHOW,0);
            if (currentIndex == 0) {
                checkDocumentOverviewFragment();
            }else if (currentIndex == 1) {
                checkMaterialFragment();
            }else if (currentIndex == 2) {
                checkSubmitFragment();
            }

            //注意，添加顺序要跟下面添加的顺序一样！！！！
            fragments.removeAll(fragments);
            fragments.add(fragmentManager.findFragmentByTag(0+""));
            fragments.add(fragmentManager.findFragmentByTag(1+""));
            fragments.add(fragmentManager.findFragmentByTag(2+""));

            //恢复fragment页面
            restoreFragment();


        }else{      //正常启动时调用
            documentOverviewText.setTextColor(getResources().getColor(R.color.base_blue));
            fragments.add(new GroupUpFrameDocumentOverviewFragment());
            fragments.add(new GroupUpFrameGroupMaterialFragment());
            fragments.add(new GroupUpFrameSubmitFragment());

            showFragment();
        }

        documentOverview.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                currentIndex = 0;
                showFragment();
                checkDocumentOverviewFragment();
            }
        });
        groupMaterial.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                currentIndex = 1;
                showFragment();
                checkMaterialFragment();
            }
        });
        submit.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                currentIndex = 2;
                showFragment();
                checkSubmitFragment();
            }
        });

    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {

        //“内存重启”时保存当前的fragment名字
        outState.putInt(STATE_FRAGMENT_SHOW,currentIndex);
        super.onSaveInstanceState(outState);
    }
    /**
     * 使用show() hide()切换页面
     * 显示fragment
     */
    private void showFragment(){

        FragmentTransaction transaction = fragmentManager.beginTransaction();

        //如果之前没有添加过
        if(!fragments.get(currentIndex).isAdded()){
            transaction
                    .hide(currentFragment)
                    .add(R.id.frameLayout,fragments.get(currentIndex),""+currentIndex);  //第三个参数为添加当前的fragment时绑定一个tag

        }else{
            transaction
                    .hide(currentFragment)
                    .show(fragments.get(currentIndex));
        }

        currentFragment = fragments.get(currentIndex);

        transaction.commit();

    }
    /**
     * 恢复fragment
     */
    private void restoreFragment(){

        FragmentTransaction mBeginTreansaction = fragmentManager.beginTransaction();

        for (int i = 0; i < fragments.size(); i++) {

            if(i == currentIndex){
                mBeginTreansaction.show(fragments.get(i));
            }else{
                mBeginTreansaction.hide(fragments.get(i));
            }

        }

        mBeginTreansaction.commit();

        //把当前显示的fragment记录下来
        currentFragment = fragments.get(currentIndex);

    }
    private void checkDocumentOverviewFragment () {
        title.setText(R.string.activity_group_up_frame_overview_document_overview);
        documentOverviewText.setTextColor(getResources().getColor(R.color.base_blue));
        groupMaterialText.setTextColor(getResources().getColor(R.color.activity_main_function_name));
        submitText.setTextColor(getResources().getColor(R.color.activity_main_function_name));
    }
    private void checkMaterialFragment () {
        title.setText(R.string.activity_group_up_frame_overview_group_material);
        groupMaterialText.setTextColor(getResources().getColor(R.color.base_blue));
        documentOverviewText.setTextColor(getResources().getColor(R.color.activity_main_function_name));
        submitText.setTextColor(getResources().getColor(R.color.activity_main_function_name));
    }

    private void checkSubmitFragment () {
        title.setText(R.string.activity_out_storage_sale_confirm_title);
        submitText.setTextColor(getResources().getColor(R.color.base_blue));
        documentOverviewText.setTextColor(getResources().getColor(R.color.activity_main_function_name));
        groupMaterialText.setTextColor(getResources().getColor(R.color.activity_main_function_name));
    }
}
